package impl.persons;

import framework.Business;
import framework.PersonAdapter;
import org.apache.log4j.Logger;

import java.util.HashMap;
import java.util.Map;

/** Implements "GRIM trigger with forgiveness after N turns" personal strategy. */
public class FTrigger extends PersonAdapter {
    private static Logger LOG = Logger.getLogger(FTrigger.class);
    private Map<Object, Integer> defects = new HashMap<Object, Integer>();
    private int n;

    public FTrigger(int turnsForForgive) {
        n = turnsForForgive;
    }

    public String getShortName() {
        return "FGRM";
    }

    @Override
    public int deal(Object id) {
        return defects.containsKey(id) ? Business.DEFECT : Business.COOPERATE;
    }

    @Override
    public void dealed(Object id, int deal) {
        if (deal < Business.NEUTRAL) {
            //DEFECT
            defects.put(id, 0);
        } else {
            //COOPERATE
            Integer turn = defects.get(id);
            if (turn != null)
                if (turn >= n) {
                    defects.remove(id);
                } else {
                    defects.put(id, turn + 1);
                }
        }
    }
}

